package com.imoresoft.magic.app.weather;

import org.apache.commons.lang.StringUtils;

import com.ambitor.grass.util.data.IData;

public class Weather {

	private IData weather;

	public Weather(IData weather) {
		this.weather = weather;
	}

	public String getWeather1() {
		return weather.getString("weather1");
	}

	public String getWeather2() {
		return weather.getString("weather2");
	}

	public String getWind1() {
		return weather.getString("wind1");
	}

	public String getWind2() {
		return weather.getString("wind2");
	}

	public String getImg1() {
		return weather.getString("img1");
	}

	public String getImg2() {
		return weather.getString("img2");
	}

	public String getImg3() {
		return weather.getString("img3");
	}

	public String getImg4() {
		return weather.getString("img4");
	}

	public Integer getAvgTemp(Integer... temps) {
		Integer sum = 0;
		boolean isAllNull = true;
		for (Integer temp : temps)
			if (temp != null) {
				sum += temp;
				isAllNull = false;
			}
		if (isAllNull)
			return null;
		return sum / temps.length;
	}

	public Integer[] getTempArray1() {
		return getTempArray("temp1");
	}

	public Integer[] getTempArray2() {
		return getTempArray("temp2");
	}

	public Integer[] getTempArray3() {
		return getTempArray("temp3");
	}

	public Integer[] getTempArray4() {
		return getTempArray("temp4");
	}

	public Integer[] getTempArray5() {
		return getTempArray("temp5");
	}

	public String getTodayTempStr() {
		return weather.getString("temp1");
	}

	private Integer[] getTempArray(String tempStrName) {
		String tempStr = weather.getString(tempStrName);
		if (StringUtils.isBlank(tempStr)) {
			return new Integer[] { null, null };
		}

		String[] tempArr = tempStr.split("~");
		String tempStart = tempArr[0].split("℃")[0];
		String startEnd = tempArr[1].split("℃")[0];

		Integer intTempStart = null;
		Integer intTempEnd = null;
		try {
			intTempStart = Integer.parseInt(tempStart);
			intTempEnd = Integer.parseInt(startEnd);
		} catch (Exception e) {
			return new Integer[] { null, null };
		}
		if (intTempStart > intTempEnd) {
			return new Integer[] { intTempEnd, intTempStart };
		}
		return new Integer[] { intTempStart, intTempEnd };
	}

}
